package com.ngc.mall.logic;

import com.ngc.mall.bo.OrderSku;
import com.ngc.mall.bo.SkuOrderBO;
import com.ngc.mall.common.configure.exception.BusinessException;
import com.ngc.mall.domain.SkuDomain;
import com.ngc.mall.dto.OrderDTO;
import com.ngc.mall.dto.SkuInfoDTO;
import lombok.Getter;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * 订单校验类
 *
 * sku 是否下架、库存为0，sku购买数量超过库存/最大购买数量,
 *
 * @author: chenxiaolin
 * @date 2023/8/25 15:35
**/
public class OrderChecker {
    private OrderDTO order;
    private CouponChecker couponChecker;
    private List<SkuDomain> serverSkuList;
    private Integer maxLimitBuy;
    @Getter
    List<OrderSku> orderSkuList=new ArrayList<>();
    public OrderChecker(OrderDTO orderDTO, CouponChecker couponChecker, List<SkuDomain> skuDomainList,int maxLimitCount) {
        this.order=orderDTO;
        this.serverSkuList=skuDomainList;
        this.couponChecker=couponChecker;
        this.maxLimitBuy=maxLimitCount;
    }
    public Integer getTotalCount(){
       return order.getSkuInfoDTOList().stream().map(skuInfoDTO ->skuInfoDTO.getCount()).reduce(Integer::sum
       ).orElse(0);
    }
    public void isOk(){
        List<SkuOrderBO> skuOrderBOList=new ArrayList<>();
        List<SkuInfoDTO> skuInfoList=order.getSkuInfoDTOList();
        BigDecimal serverTotalPrice=new BigDecimal("0");
        skuNotSale(order.getSkuInfoDTOList().size(),serverSkuList.size());
        for(var i=0;i<skuInfoList.size();i++){
            SkuInfoDTO skuInfo=skuInfoList.get(i);
            SkuDomain sku=serverSkuList.get(i);
            this.beyondSkuStock(skuInfo,sku);
            this.beyondSkuMaxLimitBuy(skuInfo);
            serverTotalPrice=serverTotalPrice.add(this.calculateSkuTotalPrice(sku,skuInfo));
            orderSkuList.add(new OrderSku(sku,skuInfo));
            skuOrderBOList.add(SkuOrderBO
                    .builder()
                    .actualPrice(sku.getActualPrice())
                    .categoryId(sku.getCategoryId())
                    .count(skuInfo.getCount())
                    .build());
        }
        this.totalPriceIsOk(order.getTotalAmount(), serverTotalPrice);
        if(couponChecker!=null){
            couponChecker.canUsed();
            couponChecker.isOk(serverTotalPrice,skuOrderBOList);
            couponChecker.finalTotalPriceIsOk(order.getFinalPayAmount(),serverTotalPrice);
        }

    }

    public void totalPriceIsOk(BigDecimal serverTotalPrice,BigDecimal orderTotalPrice){
        if(orderTotalPrice.compareTo(serverTotalPrice)!=0){
            throw new BusinessException();
        }
    }

    public String getLeaderImg() {
        return  this.serverSkuList.get(0).getImg();
    }

    public String getLeaderTitle() {
        return this.serverSkuList.get(0).getTitle();
    }

    public void skuNotSale(int count1,int count2){
        if(count1!=count2){
            throw new BusinessException("有sku下架了");
        }
    }

    public void beyondSkuStock(SkuInfoDTO skuInfoDTO,SkuDomain skuDomain){
        if(skuInfoDTO.getCount()>skuDomain.getStock()){
            throw new BusinessException("购买数量超出库存");
        }
    }

    public void beyondSkuMaxLimitBuy(SkuInfoDTO skuInfoDTO){
        if(skuInfoDTO.getCount()>maxLimitBuy){
            throw new BusinessException("超出最大购买数量");
        }
    }

    public void containsSoldOutSku(SkuDomain sku){
        if(sku.getStock()==0){
            throw new BusinessException("库存为空");
        }
    }

    public BigDecimal calculateSkuTotalPrice(SkuDomain sku,SkuInfoDTO skuInfo){
        if(skuInfo.getCount()<=0){
            throw new BusinessException("sku数量错误");
        }
        return sku.getActualPrice().multiply(new BigDecimal(skuInfo.getCount()));
    }


}
